package com.iedu.demo.lgq.mapper;

import com.iedu.demo.lgq.entity.TbOrderReview;
import com.iedu.demo.lgq.entity.TbReviewMedia;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ReviewMapper {

    // 插入评价
    @Insert("INSERT INTO comments (item_id, user_id, content, created_at) " +
            "VALUES (#{itemId}, #{userId}, #{content}, #{createdAt})") // 将 productId 改为 itemId
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertReview(TbOrderReview review);

    // 根据商品 ID 查询评价
    @Select("SELECT * FROM comments WHERE item_id = #{itemId}") // 将 productId 改为 itemId
    List<TbOrderReview> findByItemId(@Param("itemId") Integer itemId); // 将 productId 改为 itemId

    // 根据用户 ID 查询评价
    @Select("SELECT * FROM comments WHERE user_id = #{userId}")
    List<TbOrderReview> findByUserId(@Param("userId") Integer userId);

    // 根据评价 ID 查询评价
    @Select("SELECT * FROM comments WHERE id = #{reviewId}")
    TbOrderReview findByReviewId(@Param("reviewId") Integer reviewId);

    // 更新评价
    @Update("UPDATE comments SET content = #{content}, created_at = #{createdAt} WHERE id = #{id}")
    void updateReview(TbOrderReview review);

    // 删除评价
    @Delete("DELETE FROM comments WHERE id = #{reviewId}")
    void deleteReview(@Param("reviewId") Integer reviewId);

    // 插入评价媒体文件
    @Insert("INSERT INTO comment_media (comment_id, file_url, file_type) " +
            "VALUES (#{commentId}, #{fileUrl}, #{fileType})")
    void insertReviewMedia(TbReviewMedia media);

    // 根据评价 ID 查询媒体文件
    @Select("SELECT * FROM comment_media WHERE comment_id = #{commentId}")
    List<TbReviewMedia> findMediaByCommentId(Integer commentId);

    // 删除评价的媒体文件
    @Delete("DELETE FROM comment_media WHERE comment_id = #{commentId}")
    void deleteReviewMedia(@Param("commentId") Integer commentId);
}
